<?php
namespace App\Models;

class SysRole extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "sys_user_role";
    }

    private function t() {
        return __CLASS__;
    }

    /**
     * 获取所有角色信息
     *
     * @return array|false
     */
    public function gets() {
        $query = "SELECT * FROM `sys_user_role`;";
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据角色的id值来获取角色信息
     *
     * @param int $id 角色id值
     *
     * @return array|false
     */
    public function get($id) {
        $query = 'SELECT * FROM `sys_user_role` WHERE `id`=:id LIMIT 1';
        try {
            $ret = $this->db()->query($query, ['id' => $id])->fetch();
            if($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 是否存在相同的角色名称
     *
     * @param string $name 角色名称
     * @param int    $id 排除$id进行检测,如果增加排除0
     *
     * @return bool
     */
    public function isExists($name, $id = 0) {
        try {
            $query = $this->select("COUNT(*) AS cnt")->from($this->t())->where('name=:name:', [
                'name' => $name
            ])->andWhere('id!=:id:', [
                'id' => $id
            ]);
            $ret   = $query->getQuery()->execute()->toArray();
            if($ret !== false && is_array($ret) && count($ret) > 0 && isset($ret[0]['cnt'])) {
                return $ret[0]['cnt'] > 0;
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return -500;
    }

    /**
     * 添加角色权限信息
     *
     * @param array $entity 角色权限信息
     *
     * @return int|false
     */
    public function add($entity) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $entity)) {
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 修改角色信息
     *
     * @param array $entity 角色信息
     *
     * @return int|false
     */
    public function edit($entity) {
        try {
            return $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

}
